package com.saint.news;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.saint.news.Utils.AppUtils;
import com.saint.news.Utils.GlideUtil;
import com.saint.news.Utils.HttpUtil;
import com.saint.news.bean.CatContent;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static android.view.KeyEvent.KEYCODE_DPAD_LEFT;
import static android.view.KeyEvent.KEYCODE_DPAD_RIGHT;

public class ImageListActivity extends AppCompatActivity {
    private Activity act;
    private Toast toast;
    private TextView tvTitle, tvPagerNum, tvBack, tvEmpty;
    private ImageView ivLeft, ivRight, ivContent;
    private Drawable loadingDrawable;

    private CatContent catContent;
    private Callback<String> subCatContentCall;
    private Call<String> catContentCall;
    private List<CatContent.DataBean> dataBeans;
    private int currentIndex;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_list);
        initView();
        initCallBack();
        getSubCatContent();
    }

    public static Intent newIntent(Activity act, String title, String titleColor, String catId,
                                   int titleMarginTop, int titleMarginLeft, int titleSize) {
        Intent intent = new Intent(act, ImageListActivity.class);
        intent.putExtra("title", title);
        intent.putExtra("title_color", titleColor);
        intent.putExtra("cat_id", catId);
        intent.putExtra("margin_top", titleMarginTop);
        intent.putExtra("margin_left", titleMarginLeft);
        intent.putExtra("title_size", titleSize);
        return intent;
    }

    private void initView() {
        act = this;
        tvTitle = findViewById(R.id.tv_title);
        tvPagerNum = findViewById(R.id.tv_pager_num);
        ivLeft = findViewById(R.id.iv_left);
        ivRight = findViewById(R.id.iv_right);
        ivContent = findViewById(R.id.iv_content);
        tvBack = findViewById(R.id.tv_back);
        tvBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        tvEmpty = findViewById(R.id.tv_empty);
    }

    private void initCallBack() {
        subCatContentCall = new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                if (response.isSuccessful() && null != response.body()) {
                    Log.e("HTTP", "请求结果： " + response.body());
                    catContent = new Gson().fromJson(response.body(), CatContent.class);
                    dataBeans = catContent.getData();
                    if (dataBeans.size() == 0) {
                        tvEmpty.setVisibility(View.VISIBLE);
                        tvPagerNum.setVisibility(View.GONE);
                        ivLeft.setVisibility(View.GONE);
                        ivRight.setVisibility(View.GONE);
                    } else {
                        GlideUtil.loadImg(act, dataBeans.get(0).getUrl(), ivContent);
                        tvPagerNum.setText(1 + " / " + dataBeans.size());
                        tvEmpty.setVisibility(View.GONE);
                        tvPagerNum.setVisibility(View.VISIBLE);
                        ivLeft.setVisibility(View.VISIBLE);
                        ivRight.setVisibility(View.VISIBLE);
                    }
                } else {
                    onFailure(call, new Throwable("response body is null"));
                }
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                tShort("加载失败：" + t.getMessage());
                Log.e("LOADINGData", t.getMessage());
            }
        };
    }

    private void getSubCatContent() {
        loadingDrawable = getResources().getDrawable(R.drawable.loading);
        String title = getIntent().getStringExtra("title");
        String titleColor = getIntent().getStringExtra("title_color");
        int titleMarginTop = getIntent().getIntExtra("margin_top", 0);
        int titleMarginLeft = getIntent().getIntExtra("margin_left", 0);
        int titleSize = getIntent().getIntExtra("title_size", 0);
        String catId = getIntent().getStringExtra("cat_id");


        Map<String, String> map = new HashMap<>();
        map.put("cat_id", catId);

        catContentCall = HttpUtil.getInstance(MainActivity.url).getSubCatContent(map);
        catContentCall.enqueue(subCatContentCall);

        if (!TextUtils.isEmpty(titleColor) && titleColor.length() == 6) {
            tvTitle.setTextColor(Color.parseColor("#" + titleColor));
        }
        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) tvTitle.getLayoutParams();
        if (titleMarginLeft > 0) {
            params.leftMargin = titleMarginLeft;
            params.rightMargin = titleMarginLeft;
        }
        if (titleMarginTop > 0) {
            params.topMargin = titleMarginTop;
        }
        tvTitle.setLayoutParams(params);
        if (titleSize > 0) {
            tvTitle.setTextSize(titleSize);
        }
        tvTitle.setText(title);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KEYCODE_DPAD_LEFT) {
//            Log.e("OnKeyDown", "KEYCODE_DPAD_LEFT");
            if (currentIndex != 0 && dataBeans.size() > 0) {
                if (currentIndex > 0) {
                    currentIndex--;
                }
                GlideUtil.loadImg(act, dataBeans.get(currentIndex).getUrl(), ivContent);
                tvPagerNum.setText(currentIndex + 1 + " / " + dataBeans.size());
            }
        } else if (keyCode == KEYCODE_DPAD_RIGHT) {
//            Log.e("OnKeyDown", "KEYCODE_DPAD_RIGHT");
            if (dataBeans.size() > 0) {
                if (currentIndex < dataBeans.size() - 1) {
                    currentIndex++;
                }
                GlideUtil.loadImg(act, dataBeans.get(currentIndex).getUrl(), ivContent);
                tvPagerNum.setText(currentIndex + 1 + " / " + dataBeans.size());
            }
        }

        AppUtils.play();
        return super.onKeyDown(keyCode, event);
    }

    public void tShort(String msg) {
        if (null == toast) {
            toast = Toast.makeText(this, msg, Toast.LENGTH_SHORT);
        } else {
            toast.setDuration(Toast.LENGTH_SHORT);
            toast.setText(msg);
        }

        toast.show();

    }

    @Override
    protected void onDestroy() {
        if (catContentCall != null) {
            catContentCall.cancel();
        }
        super.onDestroy();
    }
}
